//
//  PartyFullContentController.m
//  UESTCPioneer
//
//  Created by Apple on 14-9-26.
//  Copyright (c) 2014年 Sway. All rights reserved.
//

#import "PartyFullContentController.h"

@interface PartyFullContentController ()<UIScrollViewDelegate>
{
    NSString * _title;
    NSString * _content;
    NSArray *  _picArray;
    CGFloat _imgViewHeight;//图片的最大高度
    NSMutableArray * _imageViewArray;//用于存储已下载的图片
    
    UILabel * _titleLabel;
    UIScrollView * _scrollView;
    UIPageControl * _page;
    UITextView *_textView;
    UIView * _backgroundView;
    
}

@end

@implementation PartyFullContentController

-(id)initWithTitle:(NSString *)title  content:(NSString *)content  picArray:(NSArray *)picArray{
    if (self) {
        _title = title;
        _content = content;
        _picArray = picArray;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //设置背景色
    [self.view setBackgroundColor:[UIColor colorWithRed:232.0f/255.0f green:232.0f/255.0f blue:232.0f/255.0f alpha:1.0f]];
    
    //1.设置标题
    [self setFullContentTitle];
    
    if (_picArray.count > 0) {
        //2.1设置图片高度
        [self setImgViewHeight];
        //2.2设置图片
        [self setFullContentPicture];
    }
    
    //3.设置内容
    [self setFullContentContent];
}

#pragma mark - 设置标题
-(void)setFullContentTitle{
    //设置标题
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, 45)];
    //    label.layer.shadowColor= [UIColor blackColor].CGColor;//阴影颜色
    //    label.layer.shadowOffset=CGSizeMake(0,0);//阴影偏移
    //    label.layer.shadowOpacity=0.5;//阴影不透明度
    //    label.layer.shadowRadius=5.0;//阴影半径
    label.text = _title;
    [label setBackgroundColor:[UIColor colorWithRed:232.0f/255.0f green:232.0f/255.0f blue:232.0f/255.0f alpha:1.0f]];
    [label setFont:[UIFont systemFontOfSize:17]];
    label.textAlignment = NSTextAlignmentCenter;
    label.numberOfLines = 0;
    label.adjustsFontSizeToFitWidth = YES;
    _titleLabel = label;
    /*自动换行
     NSDictionary *attribute = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:18] ,NSFontAttributeName, nil];
     CGSize size = [label.text boundingRectWithSize:CGSizeMake(viewController.view.frame.size.width, 20000.0f) options: NSStringDrawingUsesLineFragmentOrigin   attributes:attribute context:nil].size;
     [label setFrame:CGRectMake(viewController.view.frame.origin.x, viewController.view.frame.origin.y,size.width,size.height)];
     
     */
    [self.view addSubview:label];
    [label setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:label
                                                          attribute:NSLayoutAttributeTop
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeTop
                                                         multiplier:1 constant:0]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-3.0-[label]-3.0-|" options:0 metrics:Nil views:NSDictionaryOfVariableBindings(label)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[label(==45)]" options:0 metrics:Nil views:NSDictionaryOfVariableBindings(label)]];
    
    UIView *bar = [[UIView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, 1)];
    bar.backgroundColor = [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1];
    [label addSubview:bar];
}
#pragma mark - 设置图片
-(void)setFullContentPicture{
    
    //1.添加UIScrollView
    [self addScrollView];
    //2.添加UIPageView
    [self addPageViewControl];
    //3.添加图片
    [self addScrollImages];
}
#pragma mark - 设置内容
-(void)setFullContentContent{
    
    UITextView *textView = [[UITextView alloc]init];
    [textView setTextColor:[UIColor darkGrayColor]];
    [textView setText:_content];
    [textView setEditable:NO];
    [textView setScrollEnabled:YES];
    [textView setFont:[UIFont systemFontOfSize:16]];
    [textView setBackgroundColor:[UIColor colorWithRed:232.0f/255.0f green:232.0f/255.0f blue:232.0f/255.0f alpha:1.0f]];
    [self.view addSubview:textView];
    [textView setTranslatesAutoresizingMaskIntoConstraints:NO];
    
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:textView
                                                          attribute:NSLayoutAttributeTop
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:_titleLabel
                                                          attribute:NSLayoutAttributeBottom
                                                         multiplier:1
                                                           constant:0]];
    if (_picArray.count>0) {
        //存在图片
        [textView addSubview:_backgroundView];
        CGFloat maxY = CGRectGetMaxY(_backgroundView.frame);
        [textView setTextContainerInset:UIEdgeInsetsMake(maxY, 0, 0, 0)];
    }
    
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:textView
                                                          attribute:NSLayoutAttributeBottom
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeBottom
                                                         multiplier:1
                                                           constant:-5]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[textView]|" options:0 metrics:Nil views:NSDictionaryOfVariableBindings(textView)]];
}

#pragma mark 添加UIScrollView
-(void)addScrollView{
    
    //1.添加背景
    UIView * backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, self.view.frame.size.width, _imgViewHeight)];
    [backgroundView setBackgroundColor:[UIColor colorWithRed:232.0f/255.0f green:232.0f/255.0f blue:232.0f/255.0f alpha:1.0f]];
    [self.view addSubview:backgroundView];
    
    //2.添加UIScrollView
    CGFloat width = backgroundView.frame.size.width;
    UIScrollView * scrollView  = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, _imgViewHeight)];
    scrollView.showsHorizontalScrollIndicator = NO;//隐藏水平滚动条
    CGSize size = scrollView.frame.size;
    
    NSInteger count = _picArray.count;
    scrollView.contentSize =CGSizeMake(count*size.width,0);
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    _scrollView = scrollView;
    
    [backgroundView addSubview:scrollView];
    
    _backgroundView = backgroundView;
}

#pragma mark  添加UIPageControl
-(void)addPageViewControl{
    
    CGFloat x = (_backgroundView.frame.size.width - 150)/2;
    CGFloat y = _backgroundView.frame.size.height * 0.95;
    UIPageControl * pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(x, y, 150, 0)];
    //    pageControl.center = CGPointMake(x,y);
    //    pageControl.bounds = CGRectMake(0, 0, 150, 0);//UIPageControl的高度是固定的
    pageControl.numberOfPages = _picArray.count;
    
    pageControl.currentPageIndicatorTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"new_feature_pagecontrol_checked_point.png"]];
    //    pageControl.pageIndicatorTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"new_feature_pagecontrol_point.png"]];
    [_backgroundView addSubview:pageControl];
    _page = pageControl;
    if (_picArray.count==1) {
        _page.enabled = NO;
    }
}

#pragma mark 添加滚动显示的图片
-(void)addScrollImages{
    CGSize size = _scrollView.frame.size;
    NSLog(@"size = %@",NSStringFromCGSize(size));
    for (int i = 0; i < _picArray.count; i ++) {
        UIImageView * imageView = [[UIImageView alloc] init];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        NSString * imageName = [baseUrl stringByAppendingPathComponent:_picArray[i]];
        NSURL * imageURL = [NSURL URLWithString:imageName];
        [imageView setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"image_default_big.png"] options:SDWebImageLowPriority | SDWebImageRetryFailed];
        imageView.frame = CGRectMake(i * size.width, 0 , size.width, size.height);
        [_scrollView addSubview:imageView];
    }
}

#pragma mark  滚动代理
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    _page.currentPage = scrollView.contentOffset.x /scrollView.frame.size.width;
}

//根据图片的大小来设置imgView的高度
-(void)setImgViewHeight{
    if (_picArray.count <=0) {
        _imgViewHeight = 0;
        return;
    }
    
    NSMutableArray *imgs = [[NSMutableArray alloc]init];
    for (int i = 0; i < _picArray.count; i++) {
        NSString *picUrlStr = [baseUrl stringByAppendingString:_picArray[i]];
        UIImageView *imgView = [[UIImageView alloc] init];
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        [imgView setImageWithURL:[NSURL URLWithString:picUrlStr]];
        if (imgView.image == nil) {
            break;
        }
        [_imageViewArray addObject:imgView];
        [imgs addObject:imgView.image];
    }
    
    _imgViewHeight = 150;
    CGFloat maxImgH = 0;
    for (int i = 0; i < imgs.count; i++) {
        UIImage *img = imgs[i];
        maxImgH = (maxImgH < img.size.height) ? img.size.height : maxImgH;
    }
    
    if (maxImgH/3 > 150 && maxImgH/3 <= 200) {
        _imgViewHeight = maxImgH / 3;
        return;
    }
    if (maxImgH/3 > 200) {
        _imgViewHeight = 200;
    }
}

@end
